//
//  TimePickerView.m
//  rentCar
//
//  Created by 123 on 15/12/25.
//  Copyright © 2015年 gg. All rights reserved.
//

#import "TimePickerView.h"

@interface TimePickerView()

@property (nonatomic,weak) UIDatePicker *timerPicker;
@property (nonatomic,weak) UIView *containView;

@property (nonatomic,weak) UILabel *titleLabel;

@end

@implementation TimePickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        UIWindow *win = [UIApplication sharedApplication].keyWindow;
        [win addSubview:self];
        
        UIView *containView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenH, ScreenW, 216)];
        containView.backgroundColor = [UIColor whiteColor];
        [self addSubview:containView];
        self.containView = containView;
        
        UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 50, 30)];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelBtn setBackgroundColor:RGBColorOne(230)];
        [cancelBtn addTarget:self action:@selector(cancelClck) forControlEvents:UIControlEventTouchUpInside];
        [containView addSubview:cancelBtn];
        
        NSString *str = @"选择生日";
        CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:Font(15)}];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((ScreenW-size.width)/2, 10, size.width, 20)];
        titleLabel.text = str;
        titleLabel.textColor = [UIColor grayColor];
        titleLabel.font = Font(15);
        self.titleLabel = titleLabel;
        [containView addSubview:titleLabel];
        
        UIButton *sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-10-50, 5, 50, 30)];
        [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [sureBtn setBackgroundColor:RGBColorOne(230)];
        [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [containView addSubview:sureBtn];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 39, ScreenW, 1)];
        lineView.backgroundColor = WineColor;
        [containView addSubview:lineView];
        
        UIDatePicker *timerPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 41, ScreenW, 175)];
        timerPicker.backgroundColor = [UIColor clearColor];
        self.timerPicker = timerPicker;
        [timerPicker addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
        [containView addSubview:timerPicker];
        self.selectDate = [NSDate date];
    }
    return self;
}
- (void)setSelectDate:(NSDate *)selectDate
{
    _selectDate = selectDate;
    self.timerPicker.date = self.selectDate;
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
}
- (void)setMaximumDate:(NSDate *)maximumDate
{
    _maximumDate = maximumDate;
    self.timerPicker.maximumDate = maximumDate;
}
- (void)setMinimumDate:(NSDate *)minimumDate
{
    _minimumDate = minimumDate;
    self.timerPicker.minimumDate = minimumDate;
}
- (void)setDatePickerMode:(UIDatePickerMode)datePickerMode
{
    _datePickerMode = datePickerMode;
    self.timerPicker.datePickerMode = datePickerMode;
}
- (void)valueChanged:(UIDatePicker *)picker
{
    self.selectDate = picker.date;
}
+ (TimePickerView *)showTimerPicker
{
    TimePickerView *picker = [[TimePickerView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    [UIView animateWithDuration:0.25 animations:^{
        picker.containView.transform = CGAffineTransformMakeTranslation(0, -216);
    }];
    return picker;
}
- (void)sureClick
{
    if (self.sureBlock) {
        self.sureBlock(self.selectDate);
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self cancel];
}
- (void)cancelClck
{
    [self cancel];
}
- (void)cancel
{
    [UIView animateWithDuration:0.25 animations:^{
        self.containView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
